package Dao;

import Bean.Basic;
import Utils.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class BasicDao {
    public static List<Basic> getBasicMessage(){
        List<Basic> list = new ArrayList<>();

        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "select * from tb_basicMessage";
            //执行sql
            pst = con.prepareStatement(sql);
            //接收结果
            rs = pst.executeQuery();
            while (rs.next()){
                Basic basic = new Basic();
                basic.setId(rs.getInt(1));
                basic.setName(rs.getString(2));
                basic.setAge(rs.getInt(3));
                basic.setSex(rs.getInt(4));
                basic.setHeadship(rs.getString(5));
                basic.setDep(rs.getString(6));
                list.add(basic);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return list;
    }

    public static boolean addBasicMessage(Basic b){

        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "insert into tb_basicMessage values(?,?,?,?,?,?) ";
            //执行sql
            pst = con.prepareStatement(sql);
            //设置参数
            pst.setInt(1,0);
            pst.setString(2,b.getName());
            pst.setInt(3,b.getAge());
            pst.setInt(4,b.getSex());
            pst.setString(5,b.getHeadship());
            pst.setString(6,b.getDep());
            //接收结果
            int i = pst.executeUpdate();
            if(i>0){
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return false;
    }
    //删除对应名字的信息   用触发器使用id
    public static boolean delInfo(int id){
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        try {
            con = JDBCUtils.getConnection();
            String sql = "delete from tb_basicMessage where id = ?";
            String sql2 = "delete from tb_contact where id = ?";
            //执行sql
            pst = con.prepareStatement(sql);
            pst.setInt(1,id);
          int i =  pst.executeUpdate();
            pst = con.prepareStatement(sql2);
            pst.setInt(1,id);
                int i2 = pst.executeUpdate();
            System.out.println();
            System.out.println(id);
                if(i>0 && i2>0){
                    return true;
                }else{
                return false;

                }

        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBCUtils.close(rs,pst,con);
        }
        return false;
    }


}
